﻿
Public Class MedicoDAO

    Public Shared Function insertarMedico(ByVal MedicoNombre As String, ByVal MedicoApellido As String, ByVal MedicoNroMatricula As Integer, ByVal TipoMatriculaMedicoId As Integer, ByVal EspecialidadMedicaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_medico"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@MedicoId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@MedicoNombre", MedicoNombre).DbType = DbType.String
                    .AddWithValue("@MedicoApellido", MedicoApellido).DbType = DbType.String
                    .AddWithValue("@MedicoNroMatricula", MedicoNroMatricula).DbType = DbType.Int32
                    .AddWithValue("@TipoMatriculaMedicoId", TipoMatriculaMedicoId).DbType = DbType.Int32
                    .AddWithValue("@EspecialidadMedicaId", EspecialidadMedicaId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerMedicos(ByVal MedicoId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_medico_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@MedicoId", MedicoId).DbType = DbType.Int32
            da.Fill(ds, "Medicos")

            Return ds.Tables("Medicos")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarMedico(ByVal MedicoId As Integer, ByVal MedicoNombre As String, ByVal MedicoApellido As String, ByVal MedicoNroMatricula As Integer, ByVal TipoMatriculaId As Integer, ByVal EspecialidadMedicaId As Integer) As Boolean

        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_medico"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@MedicoId", MedicoId).DbType = DbType.Int32
                    .AddWithValue("@MedicoNombre", MedicoNombre).DbType = DbType.String
                    .AddWithValue("@MedicoApellido", MedicoApellido).DbType = DbType.String
                    .AddWithValue("@MedicoNroMatricula", MedicoNroMatricula).DbType = DbType.Int32
                    .AddWithValue("@TipoMatriculaId", TipoMatriculaId).DbType = DbType.Int32
                    .AddWithValue("@EspecialidadMedicaId", EspecialidadMedicaId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarMedico(ByVal MedicoId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_medico"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@MedicoId", MedicoId).DbType = DbType.Int32
                    .AddWithValue("@MedicoNombre", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@MedicoApellido", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@MedicoNroMatricula", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@TipoMatriculaId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@EspecialidadMedicaId", Constante.MENOSUNO).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function buscarMedicos(ByVal TipoBusqueda As Integer, ByVal MedicoNombre As String, ByVal MedicoApellido As String, ByVal MedicoNroMatricula As Integer, ByVal TipoMatriculaMedicoId As Integer) As DataTable
        'Public Shared Function buscarMedicos(ByVal TipoBusqueda As Integer, ByVal MedicoNombre As String, ByVal MedicoApellido As String) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_medico_buscar", cn)
            With da.SelectCommand
                .CommandText = "usp_medico_buscar"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@TipoBusqueda", TipoBusqueda).DbType = DbType.Int32
                    .AddWithValue("@MedicoNombre", MedicoNombre).DbType = DbType.String
                    .AddWithValue("@MedicoApellido", MedicoApellido).DbType = DbType.String
                    .AddWithValue("@MedicoNroMatricula", MedicoNroMatricula).DbType = DbType.Int32
                    .AddWithValue("@TipoMatriculaMedicoId", TipoMatriculaMedicoId).DbType = DbType.Int32
                End With
            End With
            da.Fill(ds, "Medicos")

            Return ds.Tables("Medicos")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function
End Class
